#!/bin/sh

echo
echo ====================================================
echo grade.sh does some very simple tests on the text
echo output of your program. Passing grade.sh is
echo not a guarantee that your code is correct or
echo that it will receive a good grade. You 
echo should also do your own tests.
echo
echo Attempting to mislead the grading script
echo is not permitted and will be treated as
echo academic dishonesty.
echo ====================================================
echo
make -k
passed=0
failed=0

echo "Test part 1"
./doTest > doTest.grade.out
if (cat doTest.grade.out; echo XYZ_BREAK_TOKEN_XYZ; cat doTest.expected) | awk -f checkAll.awk | grep "checkAll-OK" > /dev/null
then
   passed=`expr 1 + $passed`
   echo OK: Basic non-preemptive looks OK
else
   failed=`expr 1 + $failed`
   echo ***********
   (cat doTest.grade.out; echo XYZ_BREAK_TOKEN_XYZ; cat doTest.expected) | awk -f checkAll.awk
   echo Basic non-preemptive looks broken
   echo ***********
fi

echo "Test part 2 (this will take 10-15 seconds)"
./doTest2 | grep -v "AlarmHelper" > doTest2.grade.out
if (cat doTest2.grade.out; echo XYZ_BREAK_TOKEN_XYZ; cat doTest2.expected) | awk -f checkAll.awk | grep "checkAll-OK" > /dev/null
then
   passed=`expr 1 + $passed`
   echo OK: Basic tests still look OK
else
   failed=`expr 1 + $failed`
   echo ***********
   (cat doTest2.grade.out; echo XYZ_BREAK_TOKEN_XYZ; cat doTest2.expected) | awk -f checkAll.awk
   echo Basic preemptive looks broken
   echo ***********
fi



#
# Should make it around at least 5 times...
# I usually get around 22 times in my tests.
# But if this fails, be sure to check it manually. Maybe
# the machine is just slow. Try increasing duration
# of test.
#
if cat doTest2.grade.out | grep "9 passes potato for 5-st/nd/rd time" > /dev/null
then
   passed=`expr 1 + $passed`
   echo 9-5 OK
else
   failed=`expr 1 + $failed`
   echo 9-5 MAY BE WRONG but check doTest2.grade.out manually it is timing sensitive 
fi


echo "Test part 3 (this may take a minute or two)"
./doTest3 | grep -v "AlarmHelper" > doTest3.grade.out
if cat doTest3.grade.out | grep "One mailbox, two threads OK" > /dev/null
then
   passed=`expr 1 + $passed`
   echo One Mailbox Two Threads OK
else
   failed=`expr 1 + $failed`
   echo One Mailbox Two Threads Broken
fi
if cat doTest3.grade.out | grep "Two mailboxen, four threads OK" > /dev/null
then
   passed=`expr 1 + $passed`
   echo Two Mailboxen Four Threads OK
else
   failed=`expr 1 + $failed`
   echo Two Mailboxen Four Threads Broken
fi

if cat doTest3.grade.out | grep "lockTests OK" > /dev/null
then
   passed=`expr 1 + $passed`
   echo lockTests OK
else
   failed=`expr 1 + $failed`
   echo lockTests Broken
fi

echo "Test part 3 contd"
if (cat doTest3.grade.out; echo XYZ_BREAK_TOKEN_XYZ; cat doTest3.expected) | awk -f checkAll.awk | grep "checkAll-OK" > /dev/null
then
   passed=`expr 1 + $passed`
   echo OK: Producer consumer tests look OK
else
   failed=`expr 1 + $failed`
   echo '***********'
   (cat doTest3.grade.out; echo XYZ_BREAK_TOKEN_XYZ; cat doTest3.expected) | awk -f checkAll.awk
   echo Producer consumer tests look broken
   echo '***********'
fi

echo "Output passed: $passed  Output failed: $failed"














